package com.bit.cfdi.web.dao.impl;


import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.NegocioGiroDAO;
import com.bit.cfdi.web.domain.Negocio;
import com.bit.cfdi.web.domain.NegocioGiro;

@Repository
public class NegocioGiroDAOImpl extends HibernateDaoSupport implements
		NegocioGiroDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<NegocioGiro> getFromNegocio(Negocio negocio)  throws DataAccessException {
		
		List<NegocioGiro> toReturn = getHibernateTemplate().find(
				" from NegocioGiro as negGiro " +
				" where negGiro.negocio=?", negocio);
		return toReturn;
	}

	@Override
	@Transactional public void removeFromNegocio(List<NegocioGiro> relations)  throws DataAccessException {
		getHibernateTemplate().deleteAll(relations);

	}

	@Override
	@Transactional public void save(NegocioGiro negocioGiro) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(negocioGiro);
	}

	@Override
	@Transactional public void removeAll(final Negocio negocio) throws DataAccessException {
		List<NegocioGiro> nG =   getFromNegocio(negocio);
		if (nG != null) {
			getHibernateTemplate().deleteAll(nG);	
		}
	}

}